package handlers

import (
	"context"
	"douyin_mini/cmd/api/rpc"
	"douyin_mini/kitex_gen/comment"
	"douyin_mini/pkg/constants"
	"douyin_mini/pkg/errno"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/hertz-contrib/jwt"
	"strconv"
)

func CommentAction(ctx context.Context, c *app.RequestContext) {
	claims := jwt.ExtractClaims(ctx, c)
	userID := int64(claims[constants.IdentityKey].(float64))
	//var queryVar struct {
	//	Token       string `json:"token" form:"token" query:"token"`
	//	VideoID     int64  `json:"video_id" form:"video_id" query:"video_id"`
	//	ActionType  int32  `json:"action_type" form:"action_type" query:"action_type"`
	//	CommentText string `json:"comment_text" form:"comment_text" query:"comment_text"`
	//	CommentID   int64  `json:"comment_id" form:"comment_id" query:"comment_id"`
	//}
	//if err := c.Bind(queryVar); err != nil {
	//	SendResponse(c, errno.ConvertErr(err), nil)
	//}
	VideoID, err1 := strconv.ParseInt(c.Query("video_id"), 10, 64)
	if err1 != nil {
		SendResponse(c, errno.ConvertErr(err1), nil)
		return
	}
	ActionType, err2 := strconv.ParseInt(c.Query("action_type"), 10, 64)
	if err2 != nil {
		SendResponse(c, errno.ConvertErr(err2), nil)
		return
	}
	CommentText := c.Query("comment_text")
	CommentIDStr := c.Query("comment_id")
	var CommentID int64
	if len(CommentIDStr) == 0 {
		CommentID = 0
	} else {
		CommentID, _ = strconv.ParseInt(CommentIDStr, 10, 64)
	}

	err := rpc.ActionComment(context.Background(), &comment.CommentActionRequest{
		UserId:      userID,
		VideoId:     VideoID,
		ActionType:  int32(ActionType),
		CommentText: &CommentText,
		CommentId:   &CommentID,
	})
	if err != nil {
		SendResponse(c, errno.ConvertErr(err), nil)
		return
	}
	SendResponse(c, errno.Success, nil)
}
